package demo;

import java.util.Stack;

public class MinStack {
    private Stack<Integer> s1;
    private Stack<Integer> minstack;


    public MinStack() {
        s1=new Stack<>();
        minstack=new Stack<>();
    }
    
    public void push(int val) {
        s1.push(val);

        if (minstack.empty()){
           minstack.push(val);
        }else {
            int tmp=minstack.peek();
            if (val<=tmp){
                minstack.push(val);
            }
        }
    }
    
    public void pop() {
        if (!s1.empty()){
            int popv= s1.pop();
            int tmp2=minstack.peek();
            if (tmp2==popv){
                minstack.pop();
            }
        }

    }
    
    public int top() {
        if(!s1.empty()){
            int ret=s1.peek();
            return ret;
        }
        return -1;
    }
    
    public int getMin() {
        if (!minstack.empty()){
            return minstack.peek();
        }
        return -1;
    }
}